const BaseIpcHandler = require('../BaseIpcHandler');
const BaseDatasetWrapper = require('../../labels/BaseDatasetWrapper');

class ConvertDatasetHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log('>> ConvertDatasetHandler params = ', params);
        const { dataset_id, to_format } = params;

        // 业务逻辑
        let result = null;
        try {
            const datasetInfo = await this.app.db.datasets.getDatasetInfo(dataset_id);
            result = await BaseDatasetWrapper.invokeConvert(this.app, to_format, datasetInfo);
        } catch (err) {
            console.error('Error convert dataset', err);
            result = { code: -1, msg: err.message };
        }

        // 返回结果
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = ConvertDatasetHandler;
